package com.byl.modules.sys.service.impl;

import com.byl.common.modules.service.BaseServiceImpl;
import com.byl.modules.sys.dao.RoleDao;
import com.byl.modules.sys.dao.UserDao;
import com.byl.modules.sys.service.RoleService;
import com.byl.sdk.utils.StringUtil;
import com.byl.sdk.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/24.
 */
@SuppressWarnings("ALL")
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleDao> implements RoleService {

    @Autowired
    RoleDao roleDao;

    @Autowired
    UserDao userDao;

    @Override
    public String queryRoleByMenuId(Serializable id) {
        return roleDao.queryRoleByMenuId(id);
    }

    @Override
    @Transactional
    public int save(Map map) {
        String id = UUIDUtils.generateUUID();
        map.put("id", id);
        //保存角色菜单
        Object obj = map.get("menus");
        if (null == obj || "".equals(obj.toString()))
            return 0;
        String[] menus = obj.toString().split(",");
        List list = Arrays.asList(menus);
        map.put("menulist", list);
        //保存角色菜单
        roleDao.saveBatchRoleMenu(map);
        //保存角色
        return super.save(map);
    }

    @Override
    public int update(Map map) {
        //先删除菜单权限
        roleDao.deleteRoleMenu(map.get("id").toString());
        //重新生成菜单权限
        Object obj = map.get("menus");
        if (null != obj && !"".equals(obj.toString())) {
            String[] menus = obj.toString().split(",");
            List list = Arrays.asList(menus);
            map.put("menulist", list);
            roleDao.saveBatchRoleMenu(map);
        }
        //更新角色
        return super.update(map);
    }

    @Override
    public int checkRole(String role_name) {
        return roleDao.checkRole(role_name);
    }

    @Override
    public String deleteRole(Serializable id) {
        //查询角色是否有用户
        String users = userDao.getUserByRole(id);
        if (!StringUtil.isBlank(users))
            return users;
        //删除角色权限
        roleDao.deleteRoleMenu(id);
        //删除角色
        int rs = roleDao.delete(id);
        if (rs > 0)
            return "success";
        else
            return "fail";
    }

    @Override
    public List queryAllRole() {
        return roleDao.queryAllRole();
    }

    @Override
    public Map getMapById(Serializable id) {
        Map map=roleDao.getMapById(id);
        Object obj=map.get("menus");
        List list=new ArrayList();
        if(null==obj){
            map.put("menus",list);
            return map;
        }
        String tmp=obj.toString();
        String []gdt=tmp.split(",");
        int len=gdt.length;
        for(int i=0;i<len;i++){
            list.add(gdt[i]);
            map.put("menus",list);
        }
        return map;
    }
}
